

# Create your views here.
from django.shortcuts import render
from rest_framework.response import Response
# Create your views here.
from rest_framework.views import APIView
from rest_framework.generics import GenericAPIView
from rest_framework.mixins import UpdateModelMixin, ListModelMixin
from apps.recruit.models import City
from apps.recruit.utils import HotCitySerializers, EnterpeisSerializer, RecruitSerializers
from apps.recruit.models import Enterprise, Recruit
import datetime

# =================================================================================

class HotEnterpeisView(APIView):
    '''热门企业'''
    def get(self, request):
        hot_enter = Enterprise.objects.order_by('-visits')[0:4]
        sers = EnterpeisSerializer(hot_enter, many=True)
        print(sers.data)
        return Response(sers.data)


# ================================================================================
# 招聘
class RecruitHotCityView(APIView):
    '''热门城市'''

    def get(self, request):
        try:
            HotCitys = City.objects.filter(ishot=1)
        except:
            return Response('查寻错误')

        # 使用序列化器序列化数据
        serilizer = HotCitySerializers(HotCitys, many=True)

        # 返回数据
        return Response(serilizer.data)



# ==========================================================================
class AddQuestCount(APIView):
    '''增加公司的访问次数'''



    def put(self, request, pk):
        try:
            Ep = Enterprise.objects.get(id=pk)
        except:
            return Response('没有公司')
        try:
            Ep.visits += 1
        except:
            return Response('跟新失败')
        else:
            Ep.save()
            return Response({'message': '更新成功', 'success': True})



# ====================================================================================

class RecruitsRecommendView(APIView):
    '''推荐职位'''


    def get(self, request):
        jobs = Recruit.objects.all()[0:4]
        sers = RecruitSerializers(jobs, many=True)
        return Response(sers.data)


# =============================================================================

class NewJobsView(APIView):
    '''最新职位'''


    def get(self, request):
        now_day = datetime.date.today()
        # 拿到十天前的
        ten_day = now_day - datetime.timedelta(15)


        #new_jobs = Recruit.objects.filter(createtime__gt=ten_day)[0:4]
        new_jobs = Recruit.objects.all()[0:4]
        sers = RecruitSerializers(new_jobs, many=True)

        return Response(sers.data)



